package com.virtualtrainer_01.viewListeners;

import com.virtualtrainer_01.dao.ExerciseDB;
import com.virtualtrainer_01.dao.GroupDB;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter {
	private Context context;
	private String[] parentList;
	private String[][] childList;

	public ExpandableListAdapter(Context context) {
		this.context = context;
		GroupDB groupDb = new GroupDB(context);
		groupDb.open();
		parentList = groupDb.getGroupNames();
		groupDb.close();
		ExerciseDB exerciseDb = new ExerciseDB(context);
		exerciseDb.open();
		childList = new String[parentList.length][];

		for (int i = 0; i < parentList.length; i++) {
			childList[i] = exerciseDb.getExerciseNames(parentList[i]);
		}
		exerciseDb.close();
	}

	@Override
	public int getGroupCount() {
		return parentList.length;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return childList[groupPosition].length;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return groupPosition;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return childList[groupPosition][childPosition];
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		TextView tv = new TextView(context);
		tv.setText(parentList[groupPosition]);
		tv.setPadding(40, 0, 0, 0);
		tv.setTextSize(40);
		return tv;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		TextView tv = new TextView(context);
		tv.setText(childList[groupPosition][childPosition]);
		tv.setPadding(60, 0, 0, 0);
		tv.setTextSize(25);
		return tv;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public String[][] getChildList() {
		return childList;
	}

	public void setChildList(String[][] childList) {
		this.childList = childList;
	}
}
